//
//  SetUpViewController.m
//  livingDesign
//
//  Created by dlios on 15-2-2.
//  Copyright (c) 2015年 怡居. All rights reserved.
//

#import "SetUpViewController.h"
#import "RegisterViewController.h"

@interface SetUpViewController ()

@property (nonatomic, retain)UISwitch *swithch;

@end

@implementation SetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    self.view.backgroundColor = [UIColor colorWithRed:167 green:201 blue:251 alpha:0.8];
    
    
   
    
    
    UIImage *image2 = [UIImage imageNamed:@"registbutton.png"];
    UIImage *image3 = [UIImage imageNamed:@"registbutton.png"];
  
    
    
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 8, self.view.frame.size.height / 7, 80, 30)];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 8 , self.view.frame.size.height / 7 + 50, 50, 30)];
    
    UITextField *textfield1 = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3, self.view.frame.size.height / 7, 150, 30)];
    UITextField *textfield2 = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3, self.view.frame.size.height / 7 + 50, 150, 30)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
   
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    label1.text = @"用户名";
    label2.text = @"密码";
    label1.textColor = [UIColor blackColor];
    label2.textColor = [UIColor blackColor];
    label1.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    label2.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    
    textfield1.placeholder = @"请输入用户名";
    textfield2.placeholder = @"请输入密码";
    textfield1.borderStyle = UITextBorderStyleRoundedRect;
    textfield2.borderStyle = UITextBorderStyleRoundedRect;
    //设置代理器
    textfield1.delegate = self;
    textfield2.delegate = self;
    //设置return按钮
    [textfield1 setReturnKeyType:UIReturnKeyNext];
    //清除按钮
    textfield1.clearButtonMode = UITextFieldViewModeWhileEditing;
    textfield2.clearButtonMode = UITextFieldViewModeWhileEditing;
    //设置tag值
    textfield1.tag = 100;
    textfield2.tag = 200;
    textfield2.secureTextEntry = YES;
    //    textfield2.keyboardType = UIKeyboardTypeNumberPad;
    
    [button1 setTitle:@"登录" forState:UIControlStateNormal];
    [button3 setTitle:@"注册" forState:UIControlStateNormal];
    
    button1.frame = CGRectMake(self.view.frame.size.width / 4,self.view.frame.size.height / 4 , image2.size.width / 3, image2.size.height / 2);
    button3.frame = CGRectMake(self.view.frame.size.width / 2, self.view.frame.size.height / 4, image3.size.width / 3, image3.size.height / 2);
    [button1 addTarget:self action:@selector(buttonClickedLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    [button3 addTarget:self action:@selector(buttonClickedRegist:) forControlEvents:UIControlEventTouchUpInside];
    
    [button1 setBackgroundImage:image2 forState:UIControlStateNormal];
    
    [button3 setBackgroundImage:image3 forState:UIControlStateNormal];
    
    button1.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    button3.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    
    [self.view addSubview:label1];
    [self.view addSubview:label2];
    [self.view addSubview:textfield1];
    [self.view addSubview:textfield2];
    [self.view addSubview:button1];
    [self.view addSubview:button3];
    
    [label1 release];
    [label2 release];
    [textfield1 release];
    [textfield2 release];
    
    
    //界面设置
    
    
    //夜间模式
    UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(self.view.frame.size.width / 15,self.view.frame.size.height  * 3 / 6 ,self.view.frame.size.width * 13 / 15, 50))];
    label.text = @"  夜间模式";
    label.font = [UIFont systemFontOfSize:17];
    label.shadowColor = [UIColor colorWithRed:167 green:201 blue:251 alpha:0.5];
    [self.view addSubview:label];
    [label release];
    //夜间模式按钮
    self.swithch = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 80, 345, 80, 30)];
    _swithch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"kk"];
    [_swithch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_swithch];
    [_swithch release];
    

    //版本信息
    UIButton *button5 =[UIButton buttonWithType:UIButtonTypeSystem];
    button5.frame = CGRectMake(self.view.frame.size.width / 15, self.view.frame.size.height * 3 / 6 + 51,self.view.frame.size.width * 13 / 15, 50);
    button5.backgroundColor = [UIColor colorWithRed:167 green:201 blue:251 alpha:0.5];
    button5.layer.cornerRadius = 0;
    [button5.layer setBorderWidth:0.3];
    button5.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//标题靠左
    [button5 setTitle:@"  版本信息                                                  ▶️" forState: UIControlStateNormal];
    [button5 addTarget:self action:@selector(buttonAction5:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button5];
    [button5 release];

    //关于我们
    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeSystem];
    button6.frame = CGRectMake(self.view.frame.size.width / 15, self.view.frame.size.height * 3 / 6 + 102, self.view.frame.size.width * 13 / 15, 50);
    button6.backgroundColor = [UIColor colorWithRed:167 green:201 blue:251 alpha:0.5];
    button6.layer.cornerRadius = 0;
    [button6.layer setBorderWidth:0.3];
    button6.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//标题靠左
    [button6 setTitle:@"  关于我们                                                  ▶️" forState:(UIControlStateNormal)];
    [button6 addTarget:self action:@selector(buttonAction6:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button6];
    [button6 release];
    
    //清除缓存
    UIButton *button7 = [UIButton buttonWithType:UIButtonTypeSystem];
    button7.frame = CGRectMake(self.view.frame.size.width / 15, self.view.frame.size.height / 2 + 153, self.view.frame.size.width * 13 / 15, 50);
    button7.backgroundColor = [UIColor colorWithRed:167 green:201 blue:251 alpha:0.5];
    [button7.layer setBorderWidth:0.3];
    button7.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//标题靠左
    [button7 setTitle:@"  清除缓存                                                  ▶️" forState:(UIControlStateNormal)];
    [button7 addTarget:self action:@selector(buttonAction7:) forControlEvents:UIControlEventTouchUpOutside];
    [self.view addSubview:button7];
    [button7 release];
    
    
    
    
    
    
    
    
}
//夜间模式按钮的方法
- (void)switchValueChanged:(UISwitch *)switchb
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:switchb.isOn forKey:@"kk"]; //将上面是否开启的这个bool值 存到 NSUserdefaults 中
    [defaults synchronize];
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"night" object:nil];//发送夜间模式消息
    
}

- (void)setOn:(BOOL)on animated:(BOOL)animated
{
    [_swithch setOn:YES animated:YES];
}// does not send action


//版本信息按钮的方法
- (void)buttonAction5:(UIButton *)button
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"版本信息" message:@"livingDesign   1.1" delegate:self cancelButtonTitle:nil otherButtonTitles:@"返回", nil];
    [alertView show];
    [alertView release];
    
    
}
//关于我们的按钮的方法
- (void)buttonAction6:(UIButton *)button
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"关于我们" message:@"尊敬的用户,你好!欢迎使用你加我来装!此App是帮助您对家居的一个参考的软件,App最终解释权归开发人员所有!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"返回", nil];
    [alertView show];
    [alertView release];
    
    
    
}
//清除缓存按钮的方法
- (void)buttonAction7:(UIButton *)button
{
    
}

//收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    UITextField *textfield1 = (UITextField *)[self.view viewWithTag:100];
    UITextField *textfield2 = (UITextField *)[self.view viewWithTag:200];
    
    if (textField == textfield1) {
        [textfield2 becomeFirstResponder];
    } else {
        [textfield2 resignFirstResponder];
    }
    
    return YES;
}

//登陆按钮的方法
- (void)buttonClickedLogin:(UIButton *)button{
    UITextField *textfield1 = (UITextField *)[self.view viewWithTag:100];
    UITextField *textfield2 = (UITextField *)[self.view viewWithTag:200];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *account = [user objectForKey:textfield1.text];
    NSString *password = [user objectForKey:[NSString stringWithFormat:@"%@password",textfield1.text]];
    if (textfield1.text.length == 0) {
        _alert = [[UIAlertView alloc] initWithTitle:@"结果" message:@"用户名不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil];
        _alert.tag = 10;
        [_alert show];
    } else if(account != nil){
       
            if (![password isEqualToString:textfield2.text]){
                NSString *s = [NSString stringWithFormat:@"密码错误, 请重新输入"];
                _alert = [[UIAlertView alloc] initWithTitle:@"结果" message:s delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil];
                _alert.tag = 20;
                [_alert show];
    }
        if ([password isEqualToString:textfield2.text]) {
            
                _alert = [[UIAlertView alloc] initWithTitle:@"恭喜" message:@"登入成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"", nil];
            _alert.tag = 40;
            [_alert show];
            }
        
    } else {
        _alert = [[UIAlertView alloc] initWithTitle:@"结果" message:@"用户不存在" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"请注册", nil];
        _alert.tag = 30;
        [_alert show];
    }

}

//跳转注册页面
- (void)buttonClickedRegist:(UIButton *)button{
     RegisterViewController *regist = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:regist animated:YES];
    [regist release];
}

//弹出页面按钮方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    UITextField *textfield1 = (UITextField *)[self.view viewWithTag:100];
    UITextField *textfield2 = (UITextField *)[self.view viewWithTag:200];
    if (alertView.tag == 10) {
        if (buttonIndex == 0) {
            [textfield1 becomeFirstResponder];
        }else {
            [textfield1 becomeFirstResponder];
        }
    } else if (alertView.tag == 20){
        [textfield2 becomeFirstResponder];
    } else if (alertView.tag == 30){
        if (buttonIndex == 0) {
            [textfield1 becomeFirstResponder];
        } else {
            RegisterViewController *regist = [[RegisterViewController alloc] init];
            [self.navigationController pushViewController:regist animated:YES];
            [regist release];
        }
    }
}

//点空白处收回键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//内存管理
- (void)dealloc{
    [_alert release];
    [_swithch release];
    [super dealloc];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
